/**
* 系统管理 用户管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>设备管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-form-item label="">
          <el-input size="small" v-model="formInline.equipmentCodeOrName" placeholder="输入设备编码或名称" clearable></el-input>
        </el-form-item>
        <el-select size="small" v-model="formInline.roomId" placeholder="请选择" clearable>
          <el-option label="全部" value=""></el-option>
          <el-option label="未关联会议室" value="1"></el-option>
          <el-option label="已关联会议室" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select size="small" v-model="formInline.equipmentStatus" placeholder="请选择" clearable>
          <el-option label="全部" value=""></el-option>
          <el-option label="正常" value="1"></el-option>
          <el-option label="停用" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
        <el-button size="small" type="success" icon="el-icon-check" @click="enable">启用</el-button>
        <el-button size="small" type="success" icon="el-icon-close" @click="disable">停用</el-button>
        <el-button size="small" type="danger" icon="el-icon-delete" @click="deleteEquipment">删除</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small"
              @selection-change="selectChange"
              :data="listData"
              highlight-current-row
              v-loading="loading"
              element-loading-text="拼命加载中"
              height="500"
              style="width: 100%;">
      <el-table-column align="center" type="selection" width="50">
      </el-table-column>
      <el-table-column align="center" prop="equipmentCode" label="设备编码" width="120">
      </el-table-column>
      <el-table-column align="center" prop="equipmentName" label="设备名称" width="120">
      </el-table-column>
      <el-table-column align="center" prop="meetingRoom.roomName" label="关联会议室" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="equipmentStatus" label="设备状态" min-width="100">
        <template slot-scope="scope">
          <span v-model="scope.row.equipmentStatus">
            {{scope.row.equipmentStatus===1?'正常':'停用'}}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="remark" label="设备说明" min-width="100">
      </el-table-column>
      <el-table-column align="center" prop="modifier" label="修改人" min-width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="modifiedTime" label="修改时间" min-width="150">
        <template slot-scope="scope">
          <div>{{scope.row.modifiedTime|timestampToTime}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="creator" label="创建人" min-width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="createTime" label="创建时间" min-width="150">
        <template slot-scope="scope">
          <div>{{scope.row.createTime|timestampToTime}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-dialog
        width="50%"
        title="选择关联会议室"
        :visible.sync="innerVisible"
        append-to-body>
        <el-form :inline="true" :model="queryMeetingRoomForm">
          <el-form-item label="搜索：">
            <el-input size="small" v-model="queryMeetingRoomForm.roomCodeOrName" placeholder="输入会议室编码或名称" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" icon="el-icon-search" @click="searchMeetingRoom">搜索</el-button>
          </el-form-item>
        </el-form>
        <el-table
          ref="singleTable"
          :data="meetingRoomList"
          highlight-current-row
          @current-change="handleCurrentChange"
          style="width: 100%"
          height="200"
          size="mini">
          <el-table-column
            type="index"
            label="序号"
            width="100"
            align="center">
          </el-table-column>
          <el-table-column
            property="roomCode"
            label="会议室编码"
            align="center">
          </el-table-column>
          <el-table-column
            property="roomName"
            label="会议室名称"
            align="center">
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <Pagination v-bind:child-msg="pageparmMeetingRoom" @callFather="callFatherMeetingRoom"></Pagination>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="innerVisible = false">取消</el-button>
          <el-button size="small" type="primary" :loading="loading" class="title" @click="submitSelect">保存
          </el-button>
        </div>
      </el-dialog>

      <el-form label-width="100px" ref="editForm" :model="editForm" :rules="rules">
        <el-form-item label="设备编码：" prop="equipmentCode">
          <el-input size="small" v-model="editForm.equipmentCode" auto-complete="off" placeholder="请输入设备编码"></el-input>
        </el-form-item>
        <el-form-item label="设备名称：" prop="equipmentName">
          <el-input size="small" v-model="editForm.equipmentName" auto-complete="off" placeholder="请输入设备名称"></el-input>
        </el-form-item>
        <el-form-item label="会议室：" prop="roomId">
          <el-input size="small" v-model="meetingRoomName" auto-complete="off" placeholder="请选择关联会议室" disabled>
            <el-button slot="append" @click="chooseMeetingRoom" icon="el-icon-s-home"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="设备说明：" prop="remark">
          <el-input size="small" v-model="editForm.remark" placeholder="请输入设备说明"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog()'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    // 导入请求方法
    import Pagination from '../../components/Pagination'
    import {Get, Post} from "../../api/axiosFun";

    export default {
        data() {
            return {
                innerVisible: false,
                queryMeetingRoomForm:{
                    pageIndex: 0,
                    pageSize: 10,
                    roomCodeOrName: ''
                },
                meetingRoomList: [],
                meetingRoomName: '',
                selectMeetingRoom: {},

                nshow: true, //switch开启
                fshow: false, //switch关闭
                loading: false, //是显示加载
                title: '添加用户',
                editFormVisible: false, //控制编辑页面显示与隐藏
                // 编辑与添加
                editForm: {
                    id : '',
                    roomId : '',
                    equipmentCode : '',
                    equipmentName : '',
                    remark : ''
                },
                // 部门参数
                unitparm: {
                    userIds: '',
                    deptId: '',
                    deptName: ''
                },
                // 选择数据
                selectdata: [],
                // rules表单验证
                rules: {
                    equipmentCode: [
                        {required: true, message: '请输入设备编码', trigger: 'blur'}
                    ],
                    equipmentName: [
                        {required: true, message: '请输入设备名称', trigger: 'blur'}
                    ]
                },
                // 删除用户
                seletedata: {
                    ids: '',
                    token: localStorage.getItem('logintoken')
                },
                // 重置密码
                resetpsd: {
                    userId: '',
                    token: localStorage.getItem('logintoken')
                },
                // 用户下线
                offline: {
                    token: localStorage.getItem('logintoken')
                },
                // 请求数据参数
                formInline: {
                    pageIndex: 0,
                    pageSize: 10,
                    roomId: '',
                    equipmentCodeOrName: '',
                    equipmentStatus: ''
                },
                //用户数据
                listData: [],
                // 数据权限
                UserDept: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                // 选中
                checkmenu: [],
                //参数role
                saveroleId: '',
                // 分页参数
                pageparm: {
                    currentPage: 1,
                    pageSize: 10,
                    total: 10
                },
                pageparmMeetingRoom:{
                    currentPage: 1,
                    pageSize: 10,
                    total: 10
                }
            }
        },
        // 注册组件
        components: {
            Pagination
        },

        /**
         * 数据发生改变
         */
        watch: {},

        /**
         * 创建完毕
         */
        created() {
            this.getdata(this.formInline)
        },

        /**
         * 里面的方法只有被调用才会执行
         */
        methods: {
            // 获取数据方法
            getdata(parameter) {
                this.loading = true
                Get("/officeSystem/equipment/select", parameter)
                    .then(result => {
                        if(result.status == 200){
                            this.listData = result.data.resultList
                            // 分页赋值
                            this.pageparm.total = result.data.totalCount
                        }else{
                            this.listData = []
                            // 分页赋值
                            this.pageparm.total = result.data.totalCount
                        }
                    })
                this.loading = false
            },
            // 获取会议室列表
            searchMeetingRoom(){
                Get("/officeSystem/meetingRoom/select", this.queryMeetingRoomForm)
                    .then(result => {
                        if(result.status == 200){
                            this.pageparmMeetingRoom.total = result.data.totalCount;
                            this.meetingRoomList = result.data.resultList
                        }else{
                            this.pageparmMeetingRoom.total = result.data.totalCount;
                            this.meetingRoomList = []
                        }
                    })
            },
            // 分页插件事件
            callFather(parm) {
                this.formInline.pageIndex = parm.currentPage-1
                this.formInline.pageSize = parm.pageSize
                this.getdata(this.formInline)
            },
            callFatherMeetingRoom(parm) {
                this.queryMeetingRoomForm.pageIndex = parm.currentPage-1
                this.queryMeetingRoomForm.pageSize = parm.pageSize
                this.searchMeetingRoom(this.queryMeetingRoomForm)
            },
            chooseMeetingRoom (){
                this.searchMeetingRoom()
                this.innerVisible = true
            },
            handleCurrentChange(val) {
                this.selectMeetingRoom = val;
            },
            submitSelect(){
                this.meetingRoomName = this.selectMeetingRoom.roomName
                this.editForm.roomId = this.selectMeetingRoom.id
                this.innerVisible = false
            },
            //搜索事件
            search() {
                this.getdata(this.formInline)
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true
                if (row != undefined && row != 'undefined') {
                    this.title = '修改'
                    this.editForm.id = row.id
                    this.editForm.roomId = row.roomId
                    this.editForm.equipmentCode = row.equipmentCode
                    this.editForm.equipmentName = row.equipmentName
                    this.editForm.remark = row.remark
                    this.meetingRoomName = row.meetingRoom.roomName
                } else {
                    this.title = '添加'
                    this.editForm.id = ''
                    this.editForm.roomId = ''
                    this.editForm.equipmentCode = ''
                    this.editForm.equipmentName = ''
                    this.editForm.remark = ''
                    this.meetingRoomName = ''
                }
            },
            // 编辑、添加提交方法
            submitForm(editData) {
                this.$refs[editData].validate((valid) => {
                    if(valid){
                        if(this.title == '添加'){
                            Post("/officeSystem/equipment/insert", this.editForm)
                                .then(result => {
                                    if(result.status == 200){
                                        this.$message.success(result.message)
                                        this.editFormVisible = false;
                                    }else{
                                        this.$message.error(result.message)
                                    }
                                })
                        }else{
                            Post("/officeSystem/equipment/update", this.editForm)
                                .then(result => {
                                    if(result.status == 200){
                                        this.$message.success(result.message)
                                        this.editFormVisible = false;
                                    }else{
                                        this.$message.error(result.message)
                                    }
                                })
                        }
                        this.getdata(this.formInline)
                    }else{
                        return false;
                    }
                })
            },
            deleteEquipment(){
                if(this.selectdata.length == 0){
                    this.$message.warning("请选择要操作的数据")
                    return;
                }
                let params = {
                    params: JSON.stringify(this.selectdata)
                }
                this.$confirm('此操作将永久删除选中设备, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let params = {
                        params: JSON.stringify(this.selectdata)
                    }
                    Post("/officeSystem/equipment/delete", params)
                        .then(result => {
                            if(result.status == 200){
                                this.getdata(this.formInline)
                                this.$message.success(result.message)
                            }else{
                                this.$message.error(result.message)
                            }
                        })
                })

            },
            enable(){
                if(this.selectdata.length == 0){
                    this.$message.warning("请选择要操作的数据")
                    return;
                }
                let params = {
                    params: JSON.stringify({
                        status: 1,
                        list: this.selectdata
                    })
                }
                Post("/officeSystem/equipment/updateEquipmentStatus", params)
                    .then(result => {
                        if(result.status == 200){
                            this.getdata(this.formInline)
                            this.$message.success(result.message)
                        }else{
                            this.$message.error(result.message)
                        }
                    })
            },
            disable(){
                if(this.selectdata.length == 0){
                    this.$message.warning("请选择要操作的数据")
                    return;
                }
                let params = {
                    params: JSON.stringify({
                        status: 2,
                        list: this.selectdata
                    })
                }
                Post("/officeSystem/equipment/updateEquipmentStatus", params)
                    .then(result => {
                        if(result.status == 200){
                            this.getdata(this.formInline)
                            this.$message.success(result.message)
                        }else{
                            this.$message.error(result.message)
                        }
                    })
            },
            // 选择复选框事件
            selectChange(val) {
                this.selectdata = val
            },
            // 关闭编辑、增加弹出框
            closeDialog() {
                this.editFormVisible = false
            },
        }
    }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }
</style>

